//index.js
//获取应用实例

const app = getApp()
var util = require('../../utils/util.js')
Page({
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })

  },


  clearUserNameValue: function (e) {
    this.setData({
      userName: '',
    })

  }, 

  clearPassWordValue: function (e) {
    this.setData({
      passWord: '',
    }) 
  },

  /**
   * 上架
   */
  shelf() {
    wx.scanCode({
      success(res) {
        var app = getApp();
        app.globalData.shelfQrcode = res.result;
        //判断是否是货架是否存在
        util.getRequest('shelf/shelfInfo/' + res.result + '/true', '', 'GET', function(result) {
          if (result.success) {
            wx.navigateTo({
              url: '../home/shelf/shelf',
            })
          } else {
            wx.showToast({
              title: result.message,
              icon: 'none',
            })
          }
        })
      }
    })
  },
  /**
   * 下架
   */
  obtained() {
    wx.scanCode({
      success(res) {
        try {
          var app = getApp();
          app.globalData.shelfCode = res.result;
          wx.navigateTo({
            url: '../home/obtained/obtained',
          })
        } catch (e) {
          //todo 
        }

      }
    })
  },
  /**
   * 扫一扫查询
   */
  inquire() {
    wx.scanCode({
      success(res) {
        var app = getApp();
        app.globalData.inquireScanCode = res.result;
        wx.navigateTo({
          url: '../home/inquire/productDetails/productDetails',
        })
      }
    })
  },
  changeUser() {
    util.loginOut(function(result) {
      wx.showToast({
        title: "退出成功",
        icon: 'success',
        duration: 1000
      })
      setTimeout(function() {
        var page = getCurrentPages().pop();
        page.setData({
          phone: '',
          password: ''
        })
        // wx.navigateTo({
        //   url: '../home/logins/logins',
        // })
        page.showDialogBtn();
      }, 1000)
    }, function(result) {
      wx.showToast({
        title: '退出失败',
        icon: 'none',
        duration: 1200
      })
    })
  },
  onShow: function() {
    /**
     * 登录弹框判断
     */
    var that = this;
    var loginInfo = app.globalData.loginInfo;
    wx.getStorage({
      key: 'loginInfo',
      success: function(res) {
        loginInfo = res.data;
        if (typeof(loginInfo) != 'undefined' && loginInfo != null && loginInfo.code == 200) {
          that.hideModal();
        } else {
          that.showDialogBtn();
        }
      },
      fail() {
        that.showDialogBtn();
      }
    })
  },
  formSubmit: function(e) {
    this.login(e.detail.value);
  },

  // 登录 
  login: function(loginInfo) {
    if (loginInfo.loginName == '' || loginInfo.password == '') {
      console.log('我被点击了');
      wx.showToast({
        title: '用户名或密码错误',
        icon: 'none',
        duration: 1200
      })
    } else {
      this.loginIn(loginInfo);
      wx.showToast({
        title: '正在登录...',
        icon: 'loading',
        duration: 1200
      })

    }
  },

  onLoad: function() {},
  /**
   * 弹窗
   */
  showDialogBtn: function() {
    console.log('showDialogBtn')
    this.setData({
      showModal: true,
      showMainPage: false
    })
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function() {
    this.setData({
      showModal: false,
      showMainPage: true
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function() {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function() {
    this.hideModal();
  },

  loginIn: function(e) {
    var requestBody = e
    var result;
    util.loginRequest(requestBody, function(result) {
      if (typeof(result) != 'undefined' && result != null) {
        if (result.success) {
          wx.showToast({
            title: result.message,
            icon: 'success',
            duration: 1000
          })
          setTimeout(function() {
            var page = getCurrentPages().pop();
            page.hideModal();
          }, 800)
        } else {
          wx.clearStorage();
          wx.showToast({
            title: "用户名或密码错误",
            icon: 'none',
            duration: 1200
          })
        }
      }
    }, function() {
      wx.showToast({
        title: '登录失败',
        icon: 'none',
        duration: 1200
      })
    })
  }
})